import { ReqGetUserInfo, ReqLogout } from "@/api"
import { deleteToken } from "@/utils/auth"
import { Message } from 'element-ui';
const state = {
    userInfo: {}
}

const mutations = {
    //保存用户信息
    SAVE_USER_INFO(state, value) {
        state.userInfo = value;
    },
    // 清除用户信息
    CLEAR_USER_INFO(state) {
        state.userInfo = {};
    }
}

const actions = {
    //获取用户数据
    async getUserInfo({ commit }) {

        let result = await ReqGetUserInfo();
        // console.log(result);
        if (result === 208) {
            return Promise.reject();
        } else {
            commit("SAVE_USER_INFO", result);
        }

    },
    //退出登录
    async logout({ commit }) {
        //  清除服务器 token
        await ReqLogout();
        //  删除本地token
        deleteToken();
        //  vuex 用户信息 清除
        commit("CLEAR_USER_INFO");
        //  提示
        Message.success("退出成功！");

    }
}

const getters = {

}

export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
}